package com.shigen.demo.config;

import com.shigen.demo.interceptor.JwtInterceptor;
import com.shigen.demo.interceptor.RequestLimitInterceptor;
import javax.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

  @Resource
  private JwtInterceptor jwtInterceptor;
  @Resource
  private RequestLimitInterceptor requestLimitInterceptor;

  String[] excludePathPatterns = {
      "/swagger-resources/**",
      "/webjars/**",
      "/v2/**",
      "/swagger-ui.html/**",
      "/user/login",
      "/visitors/public/**",
      "/error",
      "/project/get/**",
      "/projectdata/insert",
      // TODO: 2022/2/19 更新头像和文件上传需要单独处理token
      "/userinfo/updateicon",
      "/BaseUserFile/upload"
  };

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(requestLimitInterceptor).addPathPatterns("/**");
    registry.addInterceptor(jwtInterceptor)
        .addPathPatterns("/**")
        .excludePathPatterns(excludePathPatterns)
    ;
  }
}
